package com.vf.admin.miaodong.controller;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppBiaoqian;
import com.vf.admin.miaodong.entity.AppZhiye;
import com.vf.admin.miaodong.entity.AppZhiyeFu;
import com.vf.admin.miaodong.service.AfterBiaoqianService;
import com.vf.admin.miaodong.service.AfterZhiyeFuService;
import com.vf.admin.miaodong.service.AfterZhiyeService;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.vf.admin.utils.StringEx.isNull;

/**
 * 职业管理
 */
@Controller
@RequestMapping("/after/zhiye")
public class AfterZhiyeController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    AfterZhiyeService afterZhiyeService;
    @Autowired
    AfterZhiyeFuService afterZhiyeFuService;


    /**
     * 查询list
     * @param code 职业编号
     * @param name 职业名
     * @param fucode 职业父级编号
     * @param funame 职业父级名
     */
    @RequestMapping("/list")
    public String list(SplitPageBean splitPageBean, String code,String name ,String fucode,String funame) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(code)) {
                paramap.put("code", code);
                request.setAttribute("code", code);

            }
            if (StringEx.isNotNull(name)) {
                paramap.put("name", name);
                request.setAttribute("name", name);

            }
            if (StringEx.isNotNull(fucode)) {
                paramap.put("fucode", fucode);
                request.setAttribute("fucode", fucode);
            }
            if (StringEx.isNotNull(funame)) {
                paramap.put("funame", funame);
                request.setAttribute("funame", funame);
            }
            List<AppZhiye> list = afterZhiyeService.list(paramap, splitPageBean);

            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/zhiye/list");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }


    /**
     * @Description 添加职业
     * @Param: [appBanner]
     * @return: com.vf.admin.common.entity.Result
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result save(AppZhiye appZhiye,Integer fu) {
        try {
            if (StringUtils.isEmpty(appZhiye.getName())) {
                return errorJSON("职业名不能为空");
            }
            if (StringUtils.isEmpty(appZhiye.getCode())) {
                return errorJSON("职业编号不能为空");
            }
            if(isNull(fu)){
                return errorJSON("职业归属不能为空");
            }
            AppZhiyeFu zhiyeFu=afterZhiyeFuService.get(fu);
            if(StringUtils.isEmpty(zhiyeFu)){
                return errorJSON("职业归属不存在");
            }
            if (appZhiye.getId() != null) {
                AppZhiye zhiye = afterZhiyeService.get(appZhiye.getId());
                zhiye.setName(appZhiye.getName());
                zhiye.setCode(appZhiye.getCode());
                zhiye.setFucode(zhiyeFu.getCode());
                zhiye.setFuname(zhiyeFu.getName());
                zhiye.setState(appZhiye.getState()==null?0:1);
                afterZhiyeService.save(zhiye);
            } else {
                AppZhiye zhiye=afterZhiyeService.getCode(appZhiye.getCode());
                if(!StringUtils.isEmpty(zhiye)){
                    return errorJSON("职业编号已存在");
                }
                appZhiye.setState(appZhiye.getState()==null?0:1);
                appZhiye.setFucode(zhiyeFu.getCode());
                appZhiye.setFuname(zhiyeFu.getName());
                afterZhiyeService.save(appZhiye);
            }
            return sucessJSON("成功");
        } catch (Exception e) {
            return errorJSON("系统繁忙");
        }
    }
    /**
     * 详情
     */
    @RequestMapping("/detail")
    public String detail(Integer id) {
        try {
            AppZhiye appZhiye = afterZhiyeService.get(id);
            Map<String,Object> map=new HashMap<>(16);
            map.put("state",1);
            List<AppZhiyeFu> fuList=afterZhiyeFuService.list(map,null);

            request.setAttribute("appZhiye", appZhiye);
            request.setAttribute("fuList", fuList);
            return this.sucess("/web/after/zhiye/edit");
        } catch (Exception e) {
            logger.error("获取详情异常", e);
            return this.error("系统繁忙");
        }
    }

}
